import { customRef } from "vue";

export default function (innitVale: string, delay: number) {
  let timer: number;
  let msg = customRef((track, trigger) => {
    //track跟踪  trigger 触发
    return {
      get() {
        track(); //告诉vue 当前值这里指的msg很重要，要持续关注 一旦msg变化就去更新
        return innitVale; //get msg被读取时调用 需要return返回值
      },
      set(value) {
        clearTimeout(timer);
        timer = setTimeout(() => {
          console.log("set");
          innitVale = value;
          trigger(); //告诉vue 关注的值变化了
        }, delay);
      }, //修改时调用
    };
  });

  return { msg };
}
